package com.woniuxy.controller;

import com.woniuxy.Exception.serviceException;
import com.woniuxy.dao.EquipmentDao;
import com.woniuxy.entity.Equipment;
import com.woniuxy.entity.EquipmentChange;
import com.woniuxy.entity.PageBean;
import com.woniuxy.entity.ResponseResult;
import com.woniuxy.service.EquipmentChangeService;
import com.woniuxy.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class EquipmentChangeController {
    @Autowired
    EquipmentChangeService equipmentChangeService;

    @Autowired
    EquipmentService equipmentService;

    @Autowired
    EquipmentDao equipmentDao;

    //路径变量参数
    //equipmentChange/{pageIndex}/{pageSize}
    //查询所有设备信息
    //通过输入设备名称查询
    @PostMapping("/equipmentChange/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<EquipmentChange>> getAllEquipmentChange(@PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize,
                                                    @RequestBody EquipmentChange equipmentChange) {
        ResponseResult<PageBean<EquipmentChange>> resEquipmentChange;
        try {
            PageBean<EquipmentChange> pageBean = equipmentChangeService.getEquipmentChangePageByCondition(equipmentChange, pageIndex, pageSize);
            resEquipmentChange = new ResponseResult<>(200, "ok", pageBean);

            return resEquipmentChange;
        } catch (serviceException e) {
            resEquipmentChange = new ResponseResult<>(500, e.getMessage(), null);
            return resEquipmentChange;
        }
    }

    //通过设备种类查询
    @PostMapping("/equipmentChangeByCategory/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<EquipmentChange>> getAllEquipmentByCategory(@PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize,
                                                    @RequestBody EquipmentChange equipmentChange) {
        ResponseResult<PageBean<EquipmentChange>> resEquipmentChange;
        try {
            PageBean<EquipmentChange> pageBean = equipmentChangeService.getEquipmentChangePageByCategory(equipmentChange, pageIndex, pageSize);
            resEquipmentChange = new ResponseResult<>(200, "ok", pageBean);

            return resEquipmentChange;
        } catch (serviceException e) {
            resEquipmentChange = new ResponseResult<>(500, e.getMessage(), null);
            return resEquipmentChange;
        }
    }

    //添加设备流通记录
    @PostMapping("/equipmentChangeAdd/")
    public ResponseResult addEquipmentChange(@RequestBody EquipmentChange equipmentChange) {

        ResponseResult resEquipmentChange;
        Equipment equipment = new Equipment();
        try {
            //添加设备表
            //将输入的设备名赋值给对象
            equipment.setEquipmentName(equipmentChange.getEquipmentName());

            //将输入的设备种类赋值给对象
            equipment.setEquipmentType(equipmentChange.getEquipmentType());

            //将输入的设备价格赋值给对象
            equipment.setPrice(equipmentChange.getPrice());
            //将设备数量赋值给对象
            equipment.setCount(equipmentChange.getCount());

            //调用方法添加设备表
            String state = equipmentChange.getState();

            equipmentService.addEquipment(equipment,state);

            //查询设备表中对应的设备，获取id
            Equipment equipment1 = equipmentDao.selectEquipment(equipment);

            //将该id赋值给equipmentChange的equipmentId
            equipmentChange.setEquipmentId(equipment1.getEquipmentId());
            //将存入状态1赋值给equipmentChange
            equipmentChange.setRemoveState(1);
            //调用方法添加流动记录
            equipmentChangeService.addEquipmentChange(equipmentChange);

            resEquipmentChange = new ResponseResult(200, "操作成功", null);
            return resEquipmentChange;
        } catch (serviceException e) {
            resEquipmentChange = new ResponseResult(500, e.getMessage(), null);
            return resEquipmentChange;
        }
    }
    //删除设备流通记录
    @DeleteMapping("/equipmentChangeRemove/{equipmentChangeId}")
    public ResponseResult removeEquipmentChange(@PathVariable("equipmentChangeId") int equipmentChangeId) {
        ResponseResult resEquipmentChange;
        try {
            //调用方法删除
            equipmentChangeService.deleteEquipmentChange(equipmentChangeId);
            resEquipmentChange = new ResponseResult(200, "删除成功", null);
            return resEquipmentChange;
        } catch (serviceException e) {
            resEquipmentChange = new ResponseResult(500, e.getMessage(), null);
            return resEquipmentChange;
        }
    }
}